<?php

/**
 * Pagina admin.php
 * Gestisce la grafica legata alle operazioni amministrative
 */
include('php/inc/global.inc.php');
include("php/module/Forum.class.php");
$search_user = new Form("POST", "admin.php#users");


if(isset($_REQUEST['update_settings'])){
    $res = Setting::updateSettings($_POST['standard_lang'],$_POST['post_page'],$_POST['forum_page']);
    
    if($res){
        KLogger::logToDB($user->username . " ha aggiornato le impostazioni del forum", $user->username, KLOGGER::FORUM);
        $smarty->assign("settings_saved", "Impostazioni aggiornate con successo");
        
        $setting = new Setting();
        $smarty->assign("setting", $setting);
        
    }
    
}

if(isset($_REQUEST['move_forum_submit'])){
    $res = Forum::moveForum($_POST['forums_name'],$_POST['move_forum_category']);
    
    if($res){
        KLogger::logToDB($user->username . " ha spostato il forum ".$_POST['forums_name'] . " nella categoria " . $_POST['move_forum_category'], $user->username, KLOGGER::FORUM);
        $smarty->assign("forum_moved", "Forum spostato con successo");
    }
    
}

if(isset($_REQUEST['delete_category'])){
    $res = Category::removeCategory($_POST['delete_category_forum']);
    
    if($res){
        KLogger::logToDB($user->username . " ha eliminato la categoria ".$_POST['delete_category_forum'], $user->username, KLOGGER::FORUM);
        $smarty->assign("category_deleted", "Categoria eliminata con successso");
    }
}

// creazione di una nuova categoria
if(isset($_REQUEST['new_category'])){
    $res = Category::newCategory($_POST['name_new_category'],$_POST['desc_new_category'], $user->username);
    
    if($res){
        KLogger::logToDB($user->username . " ha creato la categoria ".$_POST['name_new_category'], $user->username, KLOGGER::FORUM);
        $smarty->assign("category_inserted", "Categoria aggiunta con successso");
    }
    
}

// creazione di un nuovo forum
if(isset($_REQUEST['new_forum'])){
    $res = Forum::newForum($_POST['name_new_forum'],$_POST['desc_new_forum'], $_POST['new_forum_category'], $user->username);
    
    if($res){
        KLogger::logToDB($user->username . " ha creato il forum ".$_POST['name_new_forum'] . " nella categoria " . $_POST['new_forum_category'], $user->username, KLOGGER::FORUM);
        $smarty->assign("forum_inserted", "Forum aggiunto con successso");
    }
    
}

// caricamento dell'utente cercato
if(isset($_REQUEST['search_user'])){
    $user_found = User::loadUser($_POST['username']);
    if($user_found)
        KLogger::logToDB($user->username . " ha cercato l'utente ".$_POST['username'], $user->username, KLOGGER::USER);
    
    $smarty->assign("user_found", $user_found);
}


if(isset($_GET['op'])){
    if($_GET['op']=='ban'){
        
        $banned = User::ban($_GET['username']);
        $user_tmp = User::loadUser($_GET['username']);
        if($user_tmp->banned == 0)
            KLogger::logToDB($user->username . " ha sbannato l'utente ".$_GET['username'], $user->username, KLOGGER::USER);
        else
            KLogger::logToDB($user->username . " ha bannato l'utente ".$_GET['username'], $user->username, KLOGGER::USER);
        
        $smarty->assign("user_banned", $banned);
    }else if($_GET['op']=='access'){
        $access = User::getAccess($_GET['username']);
        KLogger::logToDB($user->username . " ha aperto lo storico dei login di ".$_GET['username'], $user->username, KLOGGER::USER);
        
        $smarty->assign("access", $access);
        
        // trova tutti gli utenti che hanno effettuato un accesso dall'ip che viene passato in get
        if(isset($_GET['ip'])){
            $users = Login::getAccess($_GET['ip']);
            $smarty->assign("users", $users);
            $smarty->assign("ip", $_GET['ip']);
            KLogger::logToDB($user->username . " ha controllato chi ha effettuato l'accesso dall'indirizzo IP ".$_GET['ip'], $user->username, KLOGGER::USER);
        
            
        }
    }else if($_GET['op']=='admin' || $_GET['op']=='rmadmin'){
        
        // se è admin do i permessi di amministratore all'utente aglimenti glieli revoco
        if($_GET['op']=='admin'){
            User::admin($_GET['username'], 3);
            KLogger::logToDB($user->username . " ha reso amministratore l'utente ".$_GET['username'], $user->username, KLOGGER::USER);
    
        }else{
            User::admin($_GET['username'], 1);
            KLogger::logToDB($user->username . " ha rimosso da amministratore l'utente ".$_GET['username'], $user->username, KLOGGER::USER);
        }
       
    }
    
    $user_found = User::loadUser($_GET['username']);
    $smarty->assign("user_found", $user_found);
}

$cats = Category::getCategories();

// form di ricerca utenti
$smarty->assign("openSearchUserForm", $search_user->openForm());
$smarty->assign("closeSearchUserForm", $search_user->closeForm());

$smarty->assign("search_user_label_username", $search_user->createLabel("Username"));
$smarty->assign("search_user_input_username", $search_user->createInput("username", "username", "text", "")); // al posto che(isset($_POST['name'])?$_POST['name']:"") grazie al foreach che ho commentato sopra
$smarty->assign("search_user_submit", $search_user->createInput("search_user", "search_user", "submit", "Cerca")); // al posto che(isset($_POST['name'])?$_POST['name']:"") grazie al foreach che ho commentato sopra

$new_category = new Form("POST", "admin.php#forum");

$smarty->assign("openNewCategoryForm", $new_category->openForm());
$smarty->assign("closeNewCategoryForm", $new_category->closeForm());

$smarty->assign("input_new_category_name", $new_category->createInput("name_new_category", "name_new_category", "text", "")); 
$smarty->assign("textarea_desc_new_category", $new_category->createTextArea("desc_new_category", 5, 25));
            
$smarty->assign("submit_new_category", $new_category->createInput("new_category", "new_category", "submit", "Crea categoria")); 

$delete_category = new Form("POST", "admin.php#forum");

$smarty->assign("openDeleteCategoryForm", $delete_category->openForm());
$smarty->assign("closeDeleteCategoryForm", $delete_category->closeForm());


$smarty->assign("select_delete_category_open", $delete_category->openSelect("delete_category_forum"));

$category_options = "";
foreach($cats as $cat){
    $category_options .= $delete_category->createOption($cat->name, $cat->name, "");
}
$smarty->assign("select_delete_category_options", $category_options);
$smarty->assign("select_delete_category_close", $delete_category->closeSelect());

$smarty->assign("submit_delete_category", $delete_category->createInput("delete_category", "delete_category", "submit", "Elimina categoria")); 

$new_forum = new Form("POST", "admin.php#forum");

$smarty->assign("openNewForumForm", $new_forum->openForm());
$smarty->assign("closeNewForumForm", $new_forum->closeForm());

$smarty->assign("input_new_forum_name", $new_forum->createInput("name_new_forum", "name_new_forum", "text", "")); 
$smarty->assign("textarea_desc_new_forum", $new_forum->createTextArea("desc_new_forum", 5, 25));

$smarty->assign("select_category_open", $new_forum->openSelect("new_forum_category"));

$category_options = "";
foreach($cats as $cat){
    $category_options .= $new_forum->createOption($cat->name, $cat->name, "");
}
$smarty->assign("select_category_options", $category_options);
$smarty->assign("select_category_close", $new_forum->closeSelect());
            
$smarty->assign("submit_new_forum", $new_forum->createInput("new_forum", "new_forum", "submit", "Crea forum")); 



$move_forum = new Form("POST", "admin.php#forum");

$smarty->assign("openMoveForum", $move_forum->openForm());
$smarty->assign("closeMoveForum", $move_forum->closeForm());

$smarty->assign("move_forum_name", $move_forum->createInput("forums_name", "forums_name", "text", "")); 
$smarty->assign("select_forum_move_open", $move_forum->openSelect("move_forum_category"));

$category_options = "";
foreach($cats as $cat){
    $category_options .= $move_forum->createOption($cat->name, $cat->name, "");
}
$smarty->assign("select_move_forum_options", $category_options);
$smarty->assign("select_forum_move_close", $move_forum->closeSelect());

$smarty->assign("submit_move_forum", $move_forum->createInput("move_forum_submit", "move_forum_submit", "submit", "Sposta forum"));


//$user->printUsers();
if($user->isAdmin()){
    $smarty->display($PAGES_URL . 'header.tpl');
    $smarty->display($PAGES_URL . 'menu.tpl');
    $smarty->display($PAGES_URL . 'admin.tpl');
    $smarty->display($PAGES_URL . 'footer.tpl');
}
?>
